class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int cnt = 1;
        int ans = nums[0];
        int n = nums.size();
        for (int i = 1; i < n; i++) {
            if (!cnt) {
                cnt = 1;
                ans = nums[i];
                continue;
            }
            if (nums[i] == ans) {
                cnt++;
            }
            else cnt--;
        }
        return ans;
    }
};